package zack.project.domain.activity.service.discount.Impl;

import zack.project.domain.activity.adapter.port.IActivityPort;
import zack.project.domain.activity.adapter.repository.IActivityRepository;
import zack.project.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import zack.project.domain.activity.service.discount.IDiscountCalculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author A1793
 */
@Slf4j
@Service("LUCK")
public class LuckCalculateService implements IDiscountCalculateService {

    @Resource
    IActivityRepository repository;
    @Resource
    IActivityPort port;

    @Override
    public BigDecimal calculate(String userId,
                                BigDecimal originalPrice,
                                GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {

        Integer userTakeCount = repository.queryUserTakeOutInAllActivity(userId);
        //这个是对应营销抽奖里面的活动id
        String marketExpr = groupBuyDiscount.getMarketExpr();
        //获得一个随机的折扣
        String randomAwardRate = port.getRandomAward(userTakeCount, Long.parseLong(marketExpr));
        BigDecimal payPrice = originalPrice.multiply(new BigDecimal(randomAwardRate)).setScale(0, RoundingMode.DOWN);
        if(payPrice.compareTo(BigDecimal.ZERO) <= 0){
            return new BigDecimal(0.01);
        }
        return payPrice;
    }
}
